package org.anq.ddm.controller;

import org.anq.ddm.pojo.BpmnProcess;
import org.anq.ddm.pojo.BpmnXml;
import org.anq.ddm.service.BpmnProcessService;
import org.anq.ddm.utils.ApiResult;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/process")
public class BpmnProcessController {

    @Autowired
    BpmnProcessService bpmnProcessService;


    @RequestMapping(value = "/bpmnProcess", method = RequestMethod.POST)
    public ApiResult addBpmnProcess(@RequestBody BpmnXml bx){
        ApiResult result = new ApiResult();
        BpmnProcess bp = new BpmnProcess();
        try {
            Document document = DocumentHelper.parseText(bx.getXml());
            // 获取文档根节点
            Element rootElm = document.getRootElement();
            // 取得某个节点的子节点.
            List<Element> nodes=rootElm.elements();

            Element node = nodes.get(0);

            bp.setProcesskey(node.attribute("id").getValue());
            bp.setProcessname(node.attribute("name").getValue());
            bp.setCreatetime("test-time");
            bp.setBpmnvalue(bx.getXml());
            bp.setStatus(1);
            bp.setFormid(bx.getFormKey());


            BpmnProcess bptemp = this.bpmnProcessService.findByProcessKey(bp.getProcesskey());
            if (bptemp != null) {
                bp.setId(bptemp.getId());
                this.bpmnProcessService.updateById(bp);
            } else {
                this.bpmnProcessService.save(bp);
            }

            result.setMessage("流程增加成功");
            return result;
        }catch(Exception ex){
            ex.printStackTrace();
            result.setCode(500);
            result.setMessage("流程增加失败");
            return result;
        }
    }

    /**
     * 将流程同表单（列表）绑定
     * @param processId
     * @param formKey
     * @return
     */
    @RequestMapping(value = "/bpmnProcessForm/{processId}/{formKey}", method = RequestMethod.GET)
    public ApiResult processFormList(@PathVariable Integer processId, @PathVariable String formKey) {
        if(this.bpmnProcessService.updateProcess(processId,formKey)!=0) {
            ApiResult result = new ApiResult();
            result.setMessage("绑定成功");
            return result;
        }else{
            ApiResult result = new ApiResult();
            result.setCode(500);
            result.setMessage("绑定失败");
            return result;
        }
    }

    /**
     * 流程列表
     * @return
     */
    @RequestMapping(value = "/bpmnProcesses", method = RequestMethod.GET)
    public ApiResult listProcess() {
        ApiResult result = new ApiResult();
        result.setData(this.bpmnProcessService.list());
        return result;
    }
    /**
     * 流程列表
     * @return
     */
    @RequestMapping(value = "/bpmnProcessesEnabled", method = RequestMethod.GET)
    public ApiResult listProcessEnabled() {
        ApiResult result = new ApiResult();
        result.setData(this.bpmnProcessService.findByStatus(1));
        return result;
    }


}
